<?php
/* * 
 * 自动任务系统
 */

class Admini_AutotaskController extends Uuch_ManageController {

    public function init() {
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        parent::init();
        $this->view->type = "autotask";
        $this->view->stype = 1;
    }

    public function indexAction() {
        $this->setTitle('自动任务管理');
        $pagesize = $this->getSafeParam("pagesize");
        $pagesize = $pagesize?$pagesize:10;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->getSafeParam("quick");
        $starttime = $this->getSafeParam("starttime");
        $endtime = $this->getSafeParam("endtime");
        $jobType = $this->getSafeParam("jobType");
        $jobExecute= $this->getSafeParam("jobExecute");
        $jobDisable = $this->getSafeParam("jobDisable");
        $lastStatus= $this->getSafeParam("lastStatus");
        $jobSearch = new Application_Model_Filter_AutotaskSearch();
        $jobSearch->quick = $quick;
        $jobSearch->startTime = $starttime;
        $jobSearch->endTime = $endtime;
        $jobSearch->jobType = $jobType;
        $jobSearch->jobExecute = $jobExecute;
        $jobSearch->jobDisable = $jobDisable;
        $jobSearch->lastStatus = $lastStatus;
        $result = Application_Model_Job::getFilter($jobSearch, $pagesize, $pageindex);
        $this->view->jobList = $result["list"];
        $this->view->jobAll = $result;
        $this->view->jobTypeAll = Application_Model_Job::getTypeAll();
        $this->view->jobExecuteAll = Application_Model_Job::getExecuteAll();
        $this->view->lastStatusAll = Application_Model_Job::getLastStatusAll();
        $this->view->jobDisableAll = Application_Model_Job::getDisableAll();
        $this->view->quick = $quick;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->pagesize = $pagesize;
        $this->view->jobType = $jobType;
        $this->view->jobExecute = $jobExecute;
        $this->view->jobDisable = $jobDisable;
        $this->view->lastStatus = $lastStatus;
    }

    public function addjobAction() {
        $this->setTitle('新建任务');
        $this->view->jobDisableAll = Application_Model_Job::getDisableAll();
        $this->view->jobTypeAll = Application_Model_Job::getTypeAll();
        $this->view->jobExecuteAll = Application_Model_Job::getExecuteAll();
        if (!$this->isPost()) {
            return;
        }
        $jobName = $this->getSafeParam("jobName");
        $jobDescription = $this->getSafeParam("jobDescription");
        $jobDisable = $this->getSafeParam("jobDisable");
        $jobType = $this->getSafeParam("jobType");
        $jobContent = $this->_getParam("jobContent");
        $jobExecute = $this->getSafeParam("jobExecute");
        $jobTime = $this->getSafeParam("jobTime");
        $fortime = $this->getSafeParam("fortime");
        $dateValueW = $this->_getParam("dateValueW");
        $dateValueM = $this->_getParam("dateValueM");
        if($jobName == "" || $jobDescription == "" || $jobContent == "" || ($jobTime==Application_Model_Job::Execute_ForTime && $fortime == "")){
            $this->message("任务名称、描述、内容和循环的时间不能为空！");
            return ;
        }
        if($jobExecute == Application_Model_Job::Execute_Week && count($dateValueW) == 0){
            $this->message("每周运行的日期不能为空！");
            return ;
        }
        if($jobExecute == Application_Model_Job::Execute_Month && count($dateValueM) == 0){
            $this->message("每月运行的日期不能为空！");
            return ;
        }
        $dateValue = ($jobExecute == Application_Model_Job::Execute_Week?(implode(",",$dateValueW)):($jobExecute == Application_Model_Job::Execute_Month?(implode(",",$dateValueM)):""));
        $jobItem = new Application_Model_Job();
        $jobItem->Name = $jobName;
        $jobItem->IsDisable = $jobDisable;
        $jobItem->Description = $jobDescription;
        $jobItem->Type = $jobType;
        $jobItem->Content = $jobContent;
        $jobItem->DateType = $jobExecute;
        $jobItem->Time = $jobTime;
        $jobItem->DateValue = $dateValue;
        $jobItem->TimeLog = ($jobTime==Application_Model_Job::Execute_ForTime?$fortime:"");
        $jobItem->DateCreated = date("Y-m-d H:i:s");
        $jobItem->LastStatus = Application_Model_Job::LastStatus_Default;
        $jobItem->Count = 0;
        $jobItem->save();
        $this->redirect('index');
    }

   

    public function joblogAction() {

        $this->setTitle('任务日志列表');
        $pagesize = $this->getSafeParam("pagesize");
        $pagesize = $pagesize?$pagesize:15;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $jobId = $this->_getParam("jobId");
        $startTime = $this->getSafeParam('starttime');
        $endTime = $this->getSafeParam('endtime');
        $lastStatus = $this->getSafeParam("lastStatus");
        $jobLogSearch = new Application_Model_Filter_JoblogSearch();
        $jobLogSearch->startTime = $startTime;
        $jobLogSearch->endTime = $endTime;
        $jobLogSearch->jobId = $jobId;
        $jobLogSearch->status = $lastStatus;
        $jobLogItem = Application_Model_Joblog::getByFilter($jobLogSearch, $pagesize, $pageindex);
        //给视图传数组
        $this->view->jobLogList = $jobLogItem["list"];
        $this->view->jobLogAll = $jobLogItem;
        $this->view->jobId = $jobId;
        $this->view->starttime = $startTime;
        $this->view->endtime = $endTime;
        $this->view->lastStatus = $lastStatus;
        $this->view->pagesize = $pagesize;
        $this->view->lastStatusAll = Application_Model_Job::getLastStatusAll();
        $this->view->jobAll = Application_Model_Job::getAll();
        $this->view->jobName = $jobId?Application_Model_Job::load($jobId)->Name:"";
        $this->view->stype = 4;
    }

    public function deljobAction() 
    {
        $id = $this->getSafeParam("id");
        $jobItem = Application_Model_Job::load($id);
        if(!$jobItem){
            $this->json(0);
            return ;
        }
        $jobItem->delete();
        $jobLogItem = Application_Model_Joblog::getByJobId($id);
        if(!$jobLogItem){
            $this->json(1);
            return ;
        }
        foreach ($jobLogItem as $jobLogVal) {
            $jobLogVal->delete();
        }
        $this->json(1);
    }
    
    public function jobdetailAction()
    {
        $id = $this->getSafeParam("id");
        if ($this->isPost()) {
//            var_dump($this->_getAllParams());
            $jobId = $this->getSafeParam("jobId");
            $jobItem = Application_Model_Job::load($jobId);
            $jobName = $this->getSafeParam("jobName");
            $jobDescription = $this->getSafeParam("jobDescription");
            $jobDisable = $this->getSafeParam("jobDisable");
            $jobType = $this->getSafeParam("jobType");
            $jobContent = $this->_getParam("jobContent");
            $jobExecute = $this->getSafeParam("jobExecute");
            $jobTime = $this->getSafeParam("jobTime");
            $fortime = $this->getSafeParam("fortime");
            $dateValueW = $this->_getParam("dateValueW");
            $dateValueM = $this->_getParam("dateValueM");
            if($jobName == "" || $jobDescription == "" || $jobContent == "" || ($jobTime==Application_Model_Job::Execute_ForTime && $fortime == "")){
                $this->message("任务名称、描述、内容和循环的时间不能为空！");
                return ;
            }
            if($jobExecute == Application_Model_Job::Execute_Week && count($dateValueW) == 0){
                $this->message("每周运行的日期不能为空！");
                return ;
            }
            if($jobExecute == Application_Model_Job::Execute_Month && count($dateValueM) == 0){
                $this->message("每月运行的日期不能为空！");
                return ;
            }
            $dateValue = ($jobExecute == Application_Model_Job::Execute_Week?(implode(",",$dateValueW)):($jobExecute == Application_Model_Job::Execute_Month?(implode(",",$dateValueM)):""));
            $jobItem->Name = $jobName;
            $jobItem->IsDisable = $jobDisable;
            $jobItem->Description = $jobDescription;
            $jobItem->Type = $jobType;
            $jobItem->Content = $jobContent;
            $jobItem->DateType = $jobExecute;
            $jobItem->Time = $jobTime;
            $jobItem->DateValue = $dateValue;
            $jobItem->TimeLog = ($jobTime==Application_Model_Job::Execute_ForTime?$fortime:"");
//            var_dump($jobItem);exit;
            $jobItem->save();
            $id = $jobId;
        }
        $jobItem = Application_Model_Job::load($id);
        $jobLogItem = Application_Model_Joblog::getByJobId($id);
        $this->view->jobItem = $jobItem;
        $this->view->jobLogItem = $jobLogItem;
        $this->view->jobDisableAll = Application_Model_Job::getDisableAll();
        $this->view->jobTypeAll = Application_Model_Job::getTypeAll();
        $this->view->jobExecuteAll = Application_Model_Job::getExecuteAll();
        $this->view->actype = $this->getSafeParam("actype");
    }

}
